var Respones = require("../Respones.js");
var redis_center = require("../../database/redis_center.js");
var redis_game = require("../../database/redis_game.js");
var mysql_game = require("../../database/mysql_game.js");
var utils = require("../../utils/utils.js");
var log = require("../../utils/log.js");
var game_config = require("../game_config.js");

var login_bonues_config = game_config.game_data.login_bonues_config;



function write_err(status, ret_func){
	var ret = {};
	ret[0] = status;
	ret_func(ret);
}

function check_login_bonues(uid){
	//查询数据库有无用户
	mysql_game.get_login_bonues_info(uid, function(status, data){
		if(status != Respones.OK){
			log.error("status: ", status);
			return;
		}

		if(!data || data.length <= 0){	//没有这样的key, 插入一个,发放奖励
			var bonues = login_bonues_config.bonues[0];
			mysql_game.insert_user_login_bounes(uid, bonues, function(status){
				return;
			});
		}else{
			var sql_login_bonues = data[0];
			var has_bonues = sql_login_bonues.bonues_time < utils.timestamp_today();
			if(has_bonues){		//更新本次登陆奖励
				//连续登陆了多少天
				var days = 1; 
				var is_straight = (sql_login_bonues.bonues_time >= utils.timestamp_yesterday());
				if(is_straight){
					days = sql_login_bonues.days + 1;
				}

				var index = days - 1;
				if(days > login_bonues_config.bonues.length){
					if(login_bonues_config.clear_login_straight){
						days = 1;
						index = 0;
					}else{
						index = login_bonues_config.bonues.length - 1;
					}
				}

				//发放今天的奖励
				mysql_game.update_user_login_bonues(uid, login_bonues_config.bonues[index], days, function(status){

				});
			}
			
		}
		
	});
}

function get_ugame_info_success(uid, data, ret_func){
	var ret = {};
	//登陆成功
	ret[0] = Respones.OK;
	ret[1] = data.uchip;
	ret[2] = data.uexp;
	ret[3] = data.uvip;


	redis_game.set_ugame_info_inredis(uid, {
		uchip: data.uchip,
		uexp: data.uexp,
		uvip: data.uvip,
	});

	//"NODE_GAME_WORLD_RANK"
	redis_game.update_game_world_rank("NODE_GAME_WORLD_RANK", uid, data.uchip);

	/*
	检查是否要发放登陆奖励
	 */
	check_login_bonues(uid);

	ret_func(ret);
}

function get_bonues_info_success(uid, b_has, data, ret_func){
	var ret = {};
	//登陆成功
	ret[0] = Respones.OK;
	ret[1] = b_has;		//0表示没有奖励，1表示有奖励
	if(b_has !== 1){
		ret_func(ret);
		return;
	}
	ret[2] = data.uid;
	ret[3] = data.bonues;
	ret[4] = data.days;

	ret_func(ret);
}

function get_game_info(uid, ret_func) {
	//查询数据库有无用户
	mysql_game.get_ugame_info_by_uid(uid, function(status, data){
		if(status != Respones.OK){
			write_err(status, ret_func);
			return;
		}

		if(data.length <= 0){	//没有这样的key, 注册一个
			mysql_game.insert_ugame_user(uid, game_config.game_data.first_uexp, 
							game_config.game_data.first_uchip, function(status){
				if(status != Respones.OK){
					write_err(status, ret_func);
					return;
				}
				get_game_info(uid, ret_func);
			});
		}else{
			/*
			使用nodejs连接数据库查询表后，拿到的数据是一个数组，数组里面是JSON格式的数据，但是每个JSON前面都有一个RowDataPacket
			我们可以在拿到数据后，进行一个JSON的格式化处理，这样就能把RowDataPacket给删掉
			 */
			var sql_ugame = JSON.stringify(data[0]);
			sql_ugame = JSON.parse(sql_ugame);
			//end

			if(sql_ugame.status != 0){		//status领取状态，0未领取，1已领取
				write_err(Respones.ILLEGAL_ACCOUNT, ret_func);
				return;
			}
			get_ugame_info_success(uid, sql_ugame, ret_func);
		}
		
	});
}

function get_login_bonues_info(uid, ret_func){
	//查询数据库有无用户
	mysql_game.get_login_bonues_info(uid, function(status, data){
		if(status != Respones.OK){
			write_err(status, ret_func);
			return;
		}
		if(data.length <= 0){	//没有这样的uid所对应的奖励
			get_bonues_info_success(uid, 0, sql_bonues_info, ret_func);
		}else{

			//格式化
			var sql_bonues_info = JSON.stringify(data[0]);
			sql_bonues_info = JSON.parse(sql_bonues_info);

			if(sql_bonues_info.status != 0){		//已经领取
				get_bonues_info_success(uid, 0, sql_bonues_info, ret_func);
				return;
			}
			get_bonues_info_success(uid, 1, sql_bonues_info, ret_func);
		}
	})
}

function get_rank_info_success(my_rank, rank_array, ret_func){
	var ret = {};
	ret[0] = Respones.OK;
	ret[1] = rank_array.length;
	ret[2] = rank_array;
	ret[3] = my_rank;

	ret_func(ret);
}

function recv_login_bonues_success(uid, bonuesid, bonues, ret_func){
	//更新数据库，将奖励标记为已领取
	mysql_game.updata_login_bonues_recved(bonuesid);

	//更新玩家数据库的金币数目
	mysql_game.add_ugame_uchip(uid, bonues, true);

	//更新玩家的game_redis
	redis_game.get_ugame_info_inredis(uid, function(status, ugame_info){
		if(status != Respones.OK){
			log.error("redis game get ugame info failed !!!!!!", status);
			return;
		}

		//成功获取
		ugame_info.uchip += bonues;
		redis_game.set_ugame_info_inredis(uid, ugame_info);
	});

	var ret = {
		0: Respones.OK,
		1: bonues,
	};
	ret_func(ret);
}

//领取登陆奖励
function recv_login_bonues(uid, bonuesid, ret_func){
	//查询登陆奖励合法性
	mysql_game.get_login_bonues_info(uid, function(status, data){
		if(status != Respones.OK){
			write_err(status, ret_func);
			return;
		}

		if(data.length <= 0){	//没有可领取的奖励或已领过
			write_err(Respones.INVALIDI_OPT, ret_func);
		}else{
			/*
			使用nodejs连接数据库查询表后，拿到的数据是一个数组，数组里面是JSON格式的数据，但是每个JSON前面都有一个RowDataPacket
			我们可以在拿到数据后，进行一个JSON的格式化处理，这样就能把RowDataPacket给删掉
			 */
			var sql_bonues_info = JSON.stringify(data[0]);
			sql_bonues_info = JSON.parse(sql_bonues_info);
			//end

			if(sql_bonues_info.status != 0 || sql_bonues_info.uid != bonuesid){		//status领取状态，0未领取，1已领取
				write_err(Respones.ILLEGAL_ACCOUNT, ret_func);
				return;
			}
			//更新一下金币的数目
			recv_login_bonues_success(uid, bonuesid, sql_bonues_info.bonues, ret_func);
		}
		
	});
}

function get_players_rank_info(my_uid, data, ret_func){
	var rank_array = [];
	var total_len = Math.floor(data.length / 2);
	var is_sended = false;
	var loaded = 0;
	var my_rank = -1;
	for(var i = 0; i < total_len; i++){
		rank_array.push([]);
	}

	//获取每一个在榜的玩家的信息【unick, usex, uface, uchip】
	//redis_center去获取
	var call_func = function(uid, uchip, out_array){
		redis_center.get_uinfo_inredis(uid, function(status, data){
			if(status != Respones.OK){
				if(!is_sended){
					is_sended = true;
					write_err(status, ret_func);
				}
				return;
			}

			out_array.push(data.unick);
			out_array.push(data.usex);
			out_array.push(data.uface);
			out_array.push(uchip);
			loaded ++;
			if(loaded >= total_len){
				get_rank_info_success(my_rank, rank_array, ret_func);
				return;
			}

		});
	}

	for(var i = 0, j = 0; i < data.length; i+=2, j++){
		if(my_uid == data[i]){
			my_rank = (i + 1);
		}
		call_func(data[i], data[i + 1], rank_array[j]);
	}

}

function get_world_rank_info(uid, ret_func){
	redis_game.get_world_rank_info("NODE_GAME_WORLD_RANK", 30, function(status, data){
		if(status != Respones.OK){
			write_err(status, ret_func);
			return;
		}

		//uid, uchip
		get_players_rank_info(uid, data, ret_func);
	});
}

module.exports = {
	get_game_info: get_game_info,
	get_login_bonues_info: get_login_bonues_info,
	recv_login_bonues: recv_login_bonues,
	get_world_rank_info: get_world_rank_info,
};